package com.base.themvx.pop

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.View.OnClickListener
import androidx.core.view.isVisible
import com.base.themvx.databinding.CommonHintMsgViewBinding
import com.base.themvx.defNonSyncLazy
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.enums.PopupStatus

/**
 ******************(^_^)***********************<br>
 * Author: fee(QQ/WeiXin:1176610771)<br>
 * Time: 22:03<br>
 * <P>DESC:
 * 底部弹出的提示框
 * </p>
 * ******************(^_^)***********************
 */
open class CommonBottomHintPop(context: Context) : BottomPopupView(context), OnClickListener {

    val mVB: CommonHintMsgViewBinding by defNonSyncLazy {
        CommonHintMsgViewBinding.inflate(LayoutInflater.from(context), bottomPopupContainer, true)
    }


    var mOutSideClickListener: OnClickListener? = null

    fun withInfos(
        titleRes: Int,
        subtitleRes: Int,
        cancelBtnTextRes: Int,
        confirmBtnTextRes: Int,
        outsideClickListener: OnClickListener? = null
    ): CommonBottomHintPop {
        val theVB = mVB
        theVB.tvTitle.setText(titleRes)
        theVB.tvSubtitle.setText(subtitleRes)
        val tvBtnCancel = theVB.tvBtnCancel
        if (cancelBtnTextRes == 0) {
            tvBtnCancel.isVisible = false
        } else {
            if (!tvBtnCancel.isVisible) {
                tvBtnCancel.isVisible = true
            }
            tvBtnCancel.setText(cancelBtnTextRes)
        }
        theVB.tvBtnConfirm.setText(confirmBtnTextRes)
        mOutSideClickListener = outsideClickListener
        return this
    }

    fun showAs(isInit: Boolean, buildBlock: ((builder: XPopup.Builder) -> Unit)? = null) {
        if (isInit) {
            val builder = XPopup.Builder(context)
            buildBlock?.invoke(builder)
            builder.asCustom(this)
                .show()
        } else {
            show()
        }
    }


    override fun addInnerContent() {
        mVB //让内容初始化
    }


    override fun show(): BasePopupView {
        popupStatus = PopupStatus.Dismiss
        return super.show()
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View?) {

    }

}